﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FDStudio.Core.Runtime
{
    /// <summary>
    /// 运行路径
    /// </summary>
    public class RuntimePath
    {
        private readonly string controllers = "Controllers";
        private readonly string models = "Models";
        private readonly string repositories = "Repositories";
        private readonly string services = "Services";
        private readonly string appServices = "AppServices";
        private readonly string appdata = "App_Data";
        private readonly string configName = "config.json";
        private IList<string> _commonAssembly;

        public string Root { get; set; }

        public string Web { get; set; }

        /// <summary>
        /// 控制器实例所在路径
        /// </summary>
        public string Controllers
        {
            get
            {
                var path = Path.Combine(Root, controllers);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                return path;
            }
        }

        /// <summary>
        /// 控制器实例所在路径
        /// </summary>
        public string Models
        {
            get
            {
                var path = Path.Combine(Root, models);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                return path;
            }
        }

        /// <summary>
        /// 仓储实例所在路径
        /// </summary>
        public string Repositories
        {
            get
            {
                var path = Path.Combine(Root, repositories);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                return path;
            }
        }

        /// <summary>
        /// 服务实例所在路径
        /// </summary>
        public string Services
        {
            get
            {
                var path = Path.Combine(Root, services);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                return path;
            }
        }

        /// <summary>
        /// 应用服务实例所在路径
        /// </summary>
        public string AppServices
        {
            get
            {
                var path = Path.Combine(Root, appServices);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                return path;
            }
        }

        /// <summary>
        /// App_Data所在路径
        /// </summary>
        public string AppData
        {
            get
            {
                var path = Path.Combine(Root, appdata);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                return path;
            }
        }

        /// <summary>
        /// 服务实例所在路径
        /// </summary>
        public string ConfigFile
        {
            get
            {
                return Path.Combine(AppData, configName);
            }
        }

        /// <summary>
        /// 公共程序集
        /// </summary>
        public IList<string> CommAssembly
        {
            get
            {
                if (_commonAssembly == null)
                {
                    _commonAssembly = new List<string>()
                    {
                        Path.Combine(Root,"FDStudio.Core.dll"),
                        Path.Combine(Root,"FDStudio.Core.Mvc.dll")
                    };
                }
                return _commonAssembly;
            }
        }

    }
}
